package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbType;
import com.bwie.service.TbTypeService;
import com.bwie.mapper.TbTypeMapper;
import com.bwie.utils.Result;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
* @author andy
* @description 针对表【tb_type(商品分类表)】的数据库操作Service实现
* @createDate 2025-05-24 09:02:50
*/
@Service
public class TbTypeServiceImpl extends ServiceImpl<TbTypeMapper, TbType>
    implements TbTypeService{


    @Autowired
    TbTypeMapper tbTypeMapper;

    @Autowired
    HttpServletRequest request;

    @Autowired
    FastFileStorageClient  fastFileStorageClient;

    @Override
    public Result addType(TbType tbType) {

        String token = request.getHeader("token");
        if(token == null) {
            return Result.error().setMsg("请先登录");
        }

        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String nickName = body.get("nickName", String.class);

        tbType.setCreateBy(nickName);
        tbType.setCreateTime(new Date());
        tbTypeMapper.insert(tbType);

        return Result.ok();
    }

    @Override
    public Result deleteType(Integer typeId) {

        //--2 判断分类是否存在
        TbType tbType = tbTypeMapper.selectById(typeId);
        if(tbType == null) {
            return Result.error().setMsg("分类不存在");
        }

        //--3 删除分类
        tbTypeMapper.deleteById(typeId);

        return Result.ok();
    }

    @Override
    public Result getTypeTree() {
        //--1 获取所有分类
        List<TbType> listAll = tbTypeMapper.selectList(
                new QueryWrapper<TbType>().lambda().orderByAsc(TbType::getTypeTop)
        );

        //--2 构建树形结构
        List<Tree<String>> trees = TreeUtil.build(listAll, "0", (node, tree) -> {
            tree.setId("" + node.getTypeId());
            tree.setName(node.getTypeName());
            tree.setParentId("" + node.getTypePid());
            tree.putExtra("typeImage", node.getTypeImage());
            tree.putExtra("typeStatus", node.getTypeStatus());
            tree.putExtra("typeTop", node.getTypeTop());
            tree.putExtra("createTime", node.getCreateTime());
        });

        return Result.ok().setData(trees);
    }

    @Override
    public Result setTypeStatus(Integer typeId, Integer typeStatus) {
        //--1 获取分类信息
        TbType tbType = tbTypeMapper.selectById(typeId);
        if(tbType == null) {
            return Result.error().setMsg("分类不存在");
        }

        //--2 更新分类状态
        tbType.setTypeStatus(typeStatus);
        tbTypeMapper.updateById(tbType);

        return Result.ok();
    }

    @Override
    public Result deleteTypeById(Integer typeId) {
        //--1 获取分类信息
        TbType tbType = tbTypeMapper.selectById(typeId);
        if(tbType == null) {
            return Result.error().setMsg("分类不存在");
        }

        //--2 删除分类
        tbTypeMapper.deleteById(typeId);
        return Result.ok();
    }

    @Override
    public Result uploadFile(MultipartFile file) {

        try {
            String extName = FileNameUtil.extName(file.getOriginalFilename());
            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            String url = "http://192.168.80.192:8888/" + storePath.getFullPath();
            return Result.ok().setData(url);
        } catch (IOException e) {
            e.printStackTrace();
            //throw new RuntimeException("文件上传失败");
            return Result.error().setMsg("上传失败");
        }
    }

    @Override
    public Result updateType(TbType tbType) {

        //--1 获取分类信息
        TbType type = tbTypeMapper.selectById(tbType.getTypeId());
        if(type == null) {
            return Result.error().setMsg("分类不存在");
        }

        //--2 更新分类信息
        tbTypeMapper.updateById(tbType);

        return Result.ok();
    }

    @Override
    public Result getTypeSelectTree() {
        //--1 获取所有分类
        List<TbType> listAll = tbTypeMapper.selectList(
                new QueryWrapper<TbType>().lambda().orderByAsc(TbType::getTypeTop)
        );

        //--2 构建树形结构
        List<Tree<String>> trees = TreeUtil.build(listAll, "0", (node, tree) -> {
            tree.setId("" + node.getTypeId());
            tree.setName(node.getTypeName());
            tree.setParentId("" + node.getTypePid());
            tree.putExtra("label", node.getTypeName());
            tree.putExtra("value", node.getTypeId());
            tree.putExtra("typeImage", node.getTypeImage());
            tree.putExtra("typeStatus", node.getTypeStatus());
            tree.putExtra("typeTop", node.getTypeTop());
            tree.putExtra("createTime", node.getCreateTime());
        });

        return Result.ok().setData(trees);
    }
}




